/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.MOS.mplayer;


import br.com.MOS.javacc.SDL.MOSSDLBackend;
import java.io.BufferedReader;
import java.io.PrintStream;

import java.io.InputStreamReader;
import java.io.PipedOutputStream;
import java.io.PipedInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Leonardo Baptista (cbs) - http://cbs.net.br
 */
public class MPlayer {

    public static int SHARED_KEY = 123456;
    private static PrintStream mplayerIn;
    private static BufferedReader mplayerOutErr;
    private static double m_volume;
    private static final String VARS = " -vf scale=" + MOSSDLBackend.VIDEO_LARGURA +":-2,expand="+MOSSDLBackend.VIDEO_LARGURA+":"+MOSSDLBackend.VIDEO_ALTURA+",overlay="+ SHARED_KEY;
    private static final String OPEN_MOVIE = " /home/mos/programas/res/background.avi";
    private static boolean m_rootmovie;
    //private static final String VARS = " -vf dsize="+MOSSDLBackend.VIDEO_LARGURA+":"+MOSSDLBackend.VIDEO_ALTURA+",overlay=123456,expand="+MOSSDLBackend.VIDEO_LARGURA+":"+MOSSDLBackend.VIDEO_ALTURA;
    //private static MPlayer instance;

    private MPlayer(){
        
    }

    public static void init(){
        Process mplayerProcess = null;
        m_rootmovie = true;
        try {
            mplayerProcess = Runtime.getRuntime().exec("mplayer -vo sdl -ao sdl -fs -zoom -slave -quiet -framedrop -idle" + VARS + OPEN_MOVIE);
        } catch (IOException ex) {
            Logger.getLogger(MPlayer.class.getName()).log(Level.SEVERE, null, ex);
        }

        // create the piped streams where to redirect the standard output and error of MPlayer
        // specify a bigger pipesize than the default of 1024
        PipedInputStream  readFrom = new PipedInputStream(256*1024);
        PipedOutputStream writeTo = null;
        try {
            writeTo = new PipedOutputStream(readFrom);
        } catch (IOException ex) {
            Logger.getLogger(MPlayer.class.getName()).log(Level.SEVERE, null, ex);
        }
        mplayerOutErr = new BufferedReader(new InputStreamReader(readFrom));

        // create the threads to redirect the standard output and error of MPlayer
        new EventHandler(mplayerProcess.getInputStream(), writeTo).start();
        new EventHandler(mplayerProcess.getErrorStream(), writeTo).start();

        // the standard input of MPlayer
        mplayerIn = new PrintStream(mplayerProcess.getOutputStream());
        m_volume = 6;
        setVolume(m_volume);
    }

    public static void openfile(String file){
        mplayerIn.print("loadfile \""+ file +"\" 0");
        mplayerIn.print("\n");
        mplayerIn.flush();
        m_rootmovie = false;
    }

    public static void playpause(){
        mplayerIn.print("pause");
        mplayerIn.print("\n");
        mplayerIn.flush();
    }

    public static void setVolume(double p_volume){
        mplayerIn.print("volume " + p_volume);
        mplayerIn.print("\n");
        mplayerIn.flush();
    }

    public static void maisVolume(){
        if (m_volume < 20)
            m_volume+=1;
        mplayerIn.print("volume " + m_volume);
        mplayerIn.print("\n");
        mplayerIn.flush();
    }

    public static void menosVolume(){
        if (m_volume > 0)
            m_volume-=1;
        mplayerIn.print("volume " + m_volume);
        mplayerIn.print("\n");
        mplayerIn.flush();
    }

    public static void invalidateLwuitScreen(){
        mplayerIn.print("overlay visible=1,invalidate=0:0:"+MOSSDLBackend.VIDEO_LARGURA+":"+MOSSDLBackend.VIDEO_ALTURA);
        mplayerIn.print("\n");
        mplayerIn.flush();
    }

    public static int getLength(){
        mplayerIn.print("get_property length");
        mplayerIn.print("\n");
        mplayerIn.flush();
        String answer;
        int totalTime = -1;
        try {
            while ((answer = mplayerOutErr.readLine()) != null) {
                if (answer.startsWith("ANS_length=")) {
                    totalTime = Integer.parseInt(answer.substring("ANS_length=".length()));
                    break;
                }
            }
        }
        catch (IOException e) {
        }
        return totalTime;

    }

    public static boolean isRootMovie(){
        return m_rootmovie;
    }

}
